import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/sales/api/v1';

const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '车辆档案管理'
});

const getInitDesc = formatMessage({
    id: 'api.getInit',
    defaultMessage: '初始化'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const getUpdateDesc = formatMessage({
    id: 'api.getUpdate',
    defaultMessage: '修改{resourceName}'
}, {resourceName});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出'
});

// 初始化请求
export const getInitData = () =>
    request(`${baseUrl}/ui/vehicleArchives/init`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitDesc));

// 查询车辆档案管理
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/vehicleArchives?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询车辆档案管理详情
export const getDetail = id => request(`${baseUrl}/vehicleArchives/${id}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 修改车辆档案管理
export const updateOrder = (id, data) => request(`${baseUrl}/vehicleArchives/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getUpdateDesc));

//kd车辆导入模板
export const importTemp = () => request(`${baseUrl}/vehicleArchives/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);

// 导出
export const exports = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/vehicleArchives/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};
